﻿using Newtonsoft.Json;
using RTW.OA.Areas.Personal.Servers;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.Personal.Controllers
{
    public class EMailController : Controller
    {
        private EMailServers emailServers;

        public EMailController()
        {
            emailServers = new EMailServers();
        }

        // GET: Personal/EMail
        public ActionResult Index(string boxclass)
        {
            string userId = null;
            ViewBag.EmailBoxTData = emailServers.GetBoxForUserIdToList(userId,boxclass);
            return View();
        }

        public ActionResult Edit(string id,string boxclass)
        {
            ViewData["boxclass"] = boxclass;

            string userId = null;
            ViewBag.EmailBoxTData = emailServers.GetBoxForUserIdToList(userId,boxclass);

            #region 重要程度(空-一般邮件,1-重要,2-非常重要)
            List<SelectListItem> importantlist = new List<SelectListItem>
            {
                new SelectListItem { Value = "", Text = "一般邮件" },
                new SelectListItem { Value = "1", Text = "重要" },
                new SelectListItem { Value = "2", Text = "非常重要" }
            };
            ViewBag.IMPORTANTData = importantlist;
            #endregion
            var model = new email_body();
            if (id!=null)
            {
                model = GetModel(id);
            }
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(string BODY_ID, email_body model)
        {
            #region 重要程度(空-一般邮件,1-重要,2-非常重要)
            List<SelectListItem> importantlist = new List<SelectListItem>
            {
                new SelectListItem { Value = "", Text = "一般邮件" },
                new SelectListItem { Value = "1", Text = "重要" },
                new SelectListItem { Value = "2", Text = "非常重要" }
            };
            ViewBag.IMPORTANTData = importantlist;
            #endregion

            string UserId = null;
            var name = Request["SendsState"]; //1 发送  0 草稿
            var oldboxclass= Request["Oldboxclass"];//1 收件箱 2发件箱  3草稿箱  4垃圾箱
            try
            {
                if (string.IsNullOrWhiteSpace(BODY_ID))//新增
                {
                    if (name == "1")
                    {
                        model.SEND_FLAG = "1";
                        model.SEND_TIME = DateTime.Now;
                        emailServers.Add(model,"send", UserId);
                        ViewBag.msg = "发送成功！";
                        model = new email_body();
                    }
                    else
                    {
                        model.SEND_FLAG = "0";
                        emailServers.Add(model, "drafts", UserId);
                        ViewBag.msg = "已保存到草稿箱！";
                        model = new email_body();
                    }
                }
                else//编辑
                {
                    //根据bodyid获取该数据的邮箱分类 如果是发件箱和收件箱的则进行新增 如果是草稿箱的则进行更新
                    string boxclass = emailServers.GetBoxClassForId(BODY_ID.ToString());
                    if (name == "1")//发送
                    {
                        model.SEND_FLAG = "1";
                        model.SEND_TIME = DateTime.Now;
                        if (boxclass == "发件箱" || boxclass == "收件箱")//从发件箱发送
                        {
                            emailServers.Add(model, "send", UserId);
                        }
                        else//从草稿箱发送
                        {
                            emailServers.Update(model, "send", UserId);
                        }
                        ViewBag.msg = "发送成功！";
                        model = new email_body();
                    }
                    else//存草稿
                    {
                        if (boxclass == "发件箱" || boxclass == "收件箱")//从草稿箱存
                        {
                            emailServers.Add(model, "drafts", UserId);          
                        }
                        else
                        {
                            emailServers.Update(model, "drafts", UserId);
                        }
                        ViewBag.msg = "已保存到草稿箱！";
                        model = new email_body();
                    }
                }
                return RedirectToAction("Index");
            }
            catch
            {

                ViewBag.msg = "提交出现错误！";
            }
            return View(model);
        }

        #region //GetData
        public string GetTableJsonResult(string Search,string boxClass)
        {
            var list = emailServers.GetData(Search,boxClass);
            return JsonConvert.SerializeObject(list);
        }

        public email_body GetModel(string id)
        {
            var model = emailServers.GetModelById(id);
            return model;
        }

        public ActionResult Delete(string id,string boxclass)
        {
            string UserId = null;
            if (!string.IsNullOrWhiteSpace(id))
            {
                emailServers.Del(id,boxclass, UserId);
                ViewBag.msg = "删除邮件成功！";
            }
            return Content("");
        }

        #endregion //GetData

        public ActionResult Details(string BODY_ID)
        {
            email_body model = GetModel(BODY_ID);
            ViewBag.SEND_TIME = model.SEND_TIME ;
            ViewBag.FROM_ID = model.FROM_ID;
            ViewBag.CONTENT = model.CONTENT;
            ViewBag.SUBJECT = model.SUBJECT;
            return View();
        }
    }
}